<cfcomponent output="false">
	<cfset $ = application.helpers />
	
	<!--- --->
	
	<cffunction name="saveMailing" access="public" output="true" returntype="string">
		<cfargument name="id" required="false" default="" />
		<cfargument name="name" required="true" />
		<cfargument name="from" required="true" />
		<cfargument name="subject" required="true" />
		<cfargument name="body" required="true" />
		<cfargument name="fundID" required="true" />
				
		<cfset var local = {} />
		
		<cfset sleep(300) />
		
		<!--- <cfset local.fund = application.s.fundService.getIDByFundID(arguments.fundID) /> --->
		<cfset local.fund = $.run.it('fundService.getIDByFundID', {fundID=arguments.fundID}) />
		
		<cfset local.id = $.database.save(
			table="fund_mailing",
			id=arguments.id,
			name=arguments.name,
			subject=arguments.subject,
			email_from=arguments.from,
			body=arguments.body,
			fund_ID=local.fund.id
		) />
		
		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getMailings" access="public" output="true" returntype="query">
		<cfargument name="fundID" required="true" />
		
		<cfreturn application.g.mailingGateway.getMailings(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="deleteMailing" access="public" output="true" returntype="string">
		<cfargument name="mailingID" required="true" />
		
		<cfset sleep(300) />
		
		<cfset $.database.delete(
			id=arguments.mailingID,
			table="fund_mailing"
		) />
		
		<cfreturn arguments.mailingID />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="tagContacts" access="public" output="true" returntype="string">
		<cfargument name="tag" required="true" />
		<cfargument name="mailingIDs" required="true" />
		<cfargument name="fundID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.tag = ",#arguments.tag#," />
		
		<!--- <cfset local.fund = application.s.fundService.getIDByFundID(arguments.fundID) /> --->
		<cfset local.fund = $.run.it('fundService.getIDByFundID', {fundID=arguments.fundID}) />
		
		<cfset sleep(300) />
		
		<cfquery name="local.tagContacts" datasource="fundrdb">
			update fund_contact
			set tags = #$.database.string('#local.tag#')#
			where fund_contact.active = 1
			and fund_contact.fund_id = #$.database.uuid(local.fund.id)#
			and fund_contact.id in #$.database.uuids(arguments.mailingIDs)#
		</cfquery>
		
		<!--- <cfset evaluateMailingTags(local.fund.id) /> --->
		<cfset $.run.it('mailingService.evaluateMailingTags', {fundID=local.fund.id}) />
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="clearContactTags" access="public" output="true" returntype="string">
		<cfargument name="mailingIDs" required="true" />
		<cfargument name="fundID" required="true" />
		
		<cfset var local = {} />
		
		<!--- <cfset local.fund = application.s.fundService.getIDByFundID(arguments.fundID) /> --->
		<cfset local.fund = $.run.it('fundService.getIDByFundID', {fundID=arguments.fundID}) />
		
		<cfset sleep(300) />
		
		<cfquery name="local.tagContacts" datasource="fundrdb">
			update fund_contact
			set tags = null
			where fund_contact.active = 1
			and fund_contact.fund_id = #$.database.uuid(local.fund.id)#
			and fund_contact.id in #$.database.uuids(arguments.mailingIDs)#
		</cfquery>
		
		<!--- <cfset evaluateMailingTags(local.fund.id) /> --->
		<cfset $.run.it('mailingService.evaluateMailingTags', {fundID=local.fund.id}) />
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="evaluateMailingTags" access="public" output="true" returntype="string">
		<cfargument name="fundID" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.getContactTags" datasource="fundrdb">
			select distinct
			tags 
			from fund_contact
			where fund_contact.active = 1
			and fund_id = #$.database.uuid(arguments.fundID)#
			and fund_contact.tags is not null
			and fund_contact.tags != ''
		</cfquery>
		
		<cfset local.tags = "" />
		
		<cfloop query="local.getContactTags">
			<cfloop list="#local.getContactTags.tags#" index="local.tag">
				<cfif not listContains(local.tags,local.tag)>
					<cfset local.tags = listAppend(local.tags,local.tag) />
				</cfif>
			</cfloop>
		</cfloop>
		
		<cfset $.database.save(
			table="fund",
			id=arguments.fundID,
			contact_tags=local.tags		
		) />
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getFundContactTagsByID" access="public" output="true" returntype="string">
		<cfargument name="fundID" required="true" />
		
		<cfset var local = {} />
		
		<!--- <cfset local.fund = application.s.fundService.getIDByFundID(arguments.fundID) /> --->
		<cfset $.run.it('fundService.getIDByFundID', {fundID=arguments.fundID}) />
		
		<cfset local.fundContactTags = application.g.fundGateway.getFundContactTagsByID(local.fund.id) />
		
		<cfreturn local.fundContactTags.contact_tags />
		
	</cffunction>
	
	<!--- --->

</cfcomponent>